﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FtpClient.Properties;
using System.IO;
namespace FtpClient
{
    public partial class LogForm : Form
    {
        public LogForm()
        {
            
            InitializeComponent();
            LoadHistory(CombAddress);
            LoadHistory(CombUsername);
            LoadHistory(CombPort);
            //CombHisto.Items.AddRange(new object[]{Properties.Settings.Default.userName});
            //CombHisto.Items.Add
        }

        private void BtButton_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(Properties.Settings.Default.userName);
            string ipAddr = this.CombAddress.Text.Trim();
            string port = this.CombPort.Text.Trim();
            string userName = this.CombUsername.Text.Trim();
            string password = this.txtPassword.Text.Trim();
            if (cBoxAno.Checked == false)
            {
                if (string.IsNullOrEmpty(ipAddr) || string.IsNullOrEmpty(port) || string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
                {
                    MessageBox.Show("请输入登录信息");
                    return;
                }
                MainForm form = (MainForm)this.Owner;
                form.getipAddr = ipAddr;
                form.getport = port;
                form.getuserName = userName;
                form.getpassword = password;
                Save(CombAddress);
                Save(CombUsername);
                Save(CombPort);
            }
            else
            {
                if (string.IsNullOrEmpty(ipAddr) )
                {
                    MessageBox.Show("请输入FTP地址");
                    return;
                }
                if (ipAddr=="")
                {
                    ipAddr = "21";
                }
                MainForm form = (MainForm)this.Owner;
                form.getipAddr = ipAddr;
                form.getport = port;
                form.getuserName = "anonymous";
                form.getpassword = "NULL";
                Save(CombAddress);
                Save(CombPort);
            }
            this.DialogResult = DialogResult.OK;
        }

        private void CanButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CombHisto_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        private void DeleHistory(ComboBox CombName)
        {
            string Cname = CombName.Name.ToString();
            string fileName = Path.Combine(Application.StartupPath, Cname + ".db");
           if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
           CombName.Items.Clear();
           CombName.Text = "";
        }

        private void BtDele1_Click(object sender, EventArgs e)
        {
            DeleHistory(CombAddress);
            DeleHistory(CombUsername);
            DeleHistory(CombPort);
            txtPassword.Text = "";
        }
        public void LoadHistory(ComboBox CombName)
        {
            string Cname = CombName.Name.ToString();
            string fileName = Path.Combine(Application.StartupPath, Cname+".db");
            if (File.Exists(fileName))
            {
                StreamReader r = new StreamReader(fileName, Encoding.Default);
                string name = r.ReadLine();
                while (name != null)
                {
                    CombName.AutoCompleteCustomSource.Add(name);
                    CombName.Items.Add(name);
                    name = r.ReadLine();
                }
                CombName.SelectedIndex = 0;
                r.Close();
            }
        }
        public void SaveHistory(ComboBox CombName)
        {
            string Cname = CombName.Name.ToString();
            string fileName = Path.Combine(Application.StartupPath, Cname + ".db");
            StreamWriter wr = new StreamWriter(fileName, false, Encoding.Default);
            foreach (string name in CombName.AutoCompleteCustomSource)
            {
                wr.WriteLine(name);
            }
            wr.Flush();
            wr.Close();
        }
        public void Save(ComboBox CombName)
        {
            if (!CombName.AutoCompleteCustomSource.Contains(CombName.Text))
            {
                CombName.AutoCompleteCustomSource.Add(CombName.Text);
                SaveHistory(CombName);
            }
        }
    }
}
